// 只适用于安卓
export const videoThumbnail = function(videoPath, name) {
	// 导入java类对象
	let MediaMetadataRetriever = plus.android.importClass('android.media.MediaMetadataRetriever');
	let File = plus.android.importClass('java.io.File');
	let FileOutputStream = plus.android.importClass('java.io.FileOutputStream');
	let Bitmap = plus.android.importClass('android.graphics.Bitmap');
	const pathUrl = plus.io.convertLocalFileSystemURL("_downloads/")
	console.log(pathUrl);
	//声明类对象
	let mmr = new MediaMetadataRetriever();
	let fileObj = new File(videoPath);
	let filePath;
	let fOut;
	if(!name){
		fOut = new FileOutputStream(videoPath);
	}else{
		filePath = `/storage/emulated/0/Android/data/uni.UNID36F789.red/downloads/${name}.jpg`
		fOut = new FileOutputStream(filePath);
	}

	console.log(fOut);
	// 获取本地视频
	mmr.setDataSource(fileObj.getAbsolutePath());
	console.log(videoPath);
	// 获取视频文件截图
	let bitmap = mmr.getFrameAtTime();
	console.log(bitmap != null);
	if (bitmap != null) {

		// 生成本地图片
		let p = new Promise((resolve, reject) => {
			console.log(plus.io.PRIVATE_DOC);
			plus.io.requestFileSystem(plus.io.PRIVATE_DOC, fs => {
				console.log('6666');
				fs.root.getDirectory(pathUrl, {
					create: true
				}, fileEntry => {
					console.log(777);
					console.log(fileEntry);
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut)

					console.log(filePath);
					plus.io.resolveLocalFileSystemURL('file://' + filePath, (entry) => {
							console.log(999);
							console.log(entry);
							resolve(entry)
						},
						(localErr) => {
							console.log("访问指定文件失败:" + localErr.message);
							reject(localErr.message)
							// 创建目录
						});
				}, (createError) => {
					console.log(888);
					console.log(createError);
				});
			}, function(e) {
				console.log("Request file system failed: " + e.message);
			});

		})
		return p
	} else {
		return '获取视频文件截图失败'
	}
}
